import routes from "@/views";
import CommonRouters from "./common";
import Vue from "vue";
import Router from "vue-router";
Vue.use(Router);

const router = new Router({
  routes: routes.concat(CommonRouters),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    }

    return {
      x: 0,
      y: 0
    };
  }
});

const setPageId = to => {
  const matched = to.matched.filter(m => m.name).map(m => m.name);
  document.documentElement.setAttribute("id", `page-${matched.join("-")}`);
};

router.beforeEach((to, from, next) => {
  window.scrollTo({ top: 0 });
  setPageId(to);
  next();
});

export default router;
